package com.api.gbhg.luoao.web;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.liuwenbo.utils.FormatUtil;
import com.api.gbhg.luoao.utils.PageEntity;
import com.api.gbhg.luoao.utils.VersionQuery;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import weaver.conn.RecordSet;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 事业单位离退休人员工资津贴补贴经费需求版本表
 * API
 *
 */
public class CauseRetirementPayAction {

    @POST
    @Path("/getCauseRetirementPay")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String cpapi(Map<String,String> param){
        PropertyManagementCostDao rmcd=new PropertyManagementCostDao();
        String requestid = param.get("requestId");
           //查询版本主表
        RecordSet version = VersionQuery.getversion("13", "uf_sydwltxrygzjtbtj", requestid);
        Map<String, Object>[] map = new HashMap[version.getArray().size()];
           int i = 0;
           while (version.next()) {
               map[i] = new HashMap<>();
               String xmmin = version.getString("xmmin");
               String subcompanyname = version.getString("subcompanyname");
               String departmentmart = version.getString("departmentmark");
               int jbr = version.getInt("jbr");//经办人id
               int id = version.getInt("id");//id

               map[i].put("lcjlsqjlid", version.getInt("lcjlsqjlid"));//流程id
               map[i].put("xmmin", xmmin);//合同名称
               map[i].put("sbdw", subcompanyname);//申报单位
               map[i].put("sbks", departmentmart);//部门

               BigDecimal yszje = new BigDecimal(version.getString("yszje"));
               map[i].put("yszje", yszje.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//总金额

               RecordSet userNameById = rmcd.getUserNameById(jbr);
               userNameById.next();
               String loginid = userNameById.getString("loginid");
               if (loginid == "") {
                   loginid = "系统管理员";
               }
               map[i].put("jbr", loginid);//经办人
               //查询明细表1
               RecordSet getdetail = VersionQuery.getdetail("uf_sydwltxrygzjtbtj_dt1", id);

               int count = 1;
               while (getdetail.next()) {


                   String rylx = getdetail.getString("rylx");//人员类型
                   String gnkm = "[30302]退休费";
                   if (rylx.equals("离休人员")) {
                       count = 2;
                       gnkm = "[30301]离休费";
                   }
                   map[i].put("rylx" + count, gnkm);
                   BigDecimal lxfxqxj = new BigDecimal(version.getString("lxfxqxj"));
                   map[i].put("lxfxqxj" + count, lxfxqxj.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//离休费需求小计

                   BigDecimal jblxfxq = new BigDecimal(version.getString("lxfxqxj"));
                   map[i].put("jblxfxq" + count, jblxfxq.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//基本离休费需求

                   BigDecimal zfwxj = new BigDecimal(version.getString("zfwxj"));
                   map[i].put("zfwxj" + count,  zfwxj.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//住房维修金

                   BigDecimal jhsyj = new BigDecimal(version.getString("jhsyj"));
                   map[i].put("jhsyj" + count, jhsyj.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//计划生育奖

                   BigDecimal jrjt = new BigDecimal(version.getString("jrjt"));
                   map[i].put("jrjt" + count, jrjt.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//节日津贴

                   BigDecimal nzwwj = new BigDecimal(version.getString("nzwwj"));
                   map[i].put("nzwwj" + count,  nzwwj.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//年终慰问金
                   count = 1;
               }
               i++;
           }
            PageEntity page = new PageEntity();
            page.setList(map);
            page.setFromtable("uf_sydwltxrygzjtbtj");

        return JSON.toJSONString(page);
    }

    //13 事业单位离退休人员工资津贴补贴经费需求表
    public String cpapi1(String requestid){
        PropertyManagementCostDao rmcd=new PropertyManagementCostDao();
//        String requestid = param.get("requestId");
        //查询版本主表
        RecordSet version = VersionQuery.getversion("13", "uf_sydwltxrygzjtbtj", requestid);
        Map<String, Object>[] map = new HashMap[version.getArray().size()];
        int i = 0;
        while (version.next()) {
            map[i] = new HashMap<>();
            String xmmin = version.getString("xmmin");
            String subcompanyname = version.getString("subcompanyname");
            String departmentmart = version.getString("departmentmark");
            int jbr = version.getInt("jbr");//经办人id
            int id = version.getInt("id");//id

            map[i].put("lcjlsqjlid", version.getInt("lcjlsqjlid"));//流程id
            map[i].put("xmmin", xmmin);//合同名称
            map[i].put("sbdw", subcompanyname);//申报单位
            map[i].put("sbks", departmentmart);//部门

            BigDecimal yszje = new BigDecimal(version.getString("yszje"));
            map[i].put("yszje", yszje.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//总金额

            RecordSet userNameById = rmcd.getUserNameById(jbr);
            userNameById.next();
            String lastname = userNameById.getString("lastname");
            if (lastname == "") {
                lastname = "系统管理员";
            }
            map[i].put("jbr", lastname);//经办人
            //查询明细表1
            RecordSet getdetail = VersionQuery.getdetail("uf_sydwltxrygzjtbtj_dt1", id);

            int count = 1;
            while (getdetail.next()) {


                String rylx = getdetail.getString("rylx");//人员类型
                String gnkm = "[30302]退休费";
                if (rylx.equals("离休人员")) {
                    count = 2;
                    gnkm = "[30301]离休费";
                }
                map[i].put("rylx" + count, gnkm);
                BigDecimal lxfxqxj = new BigDecimal(FormatUtil.DmFormat(getdetail.getDouble("lxfxqxj")));
                map[i].put("lxfxqxj" + count, lxfxqxj.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//离休费需求小计

                BigDecimal jblxfxq = new BigDecimal(FormatUtil.DmFormat(getdetail.getDouble("jblxfxq")));
                map[i].put("jblxfxq" + count, jblxfxq.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//基本离休费需求

                BigDecimal zfwxj = new BigDecimal(FormatUtil.DmFormat(getdetail.getDouble("zfwxj")));
                map[i].put("zfwxj" + count, zfwxj.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//住房维修金

                BigDecimal jhsyj = new BigDecimal(FormatUtil.DmFormat(getdetail.getDouble("jhsyj")));
                map[i].put("jhsyj" + count,jhsyj.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//计划生育奖

                BigDecimal jrjt = new BigDecimal(FormatUtil.DmFormat(getdetail.getDouble("jrjt")));
                map[i].put("jrjt" + count, jrjt.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//节日津贴

                BigDecimal nzwwj = new BigDecimal(FormatUtil.DmFormat(getdetail.getDouble("nzwwj")));
                map[i].put("nzwwj" + count,nzwwj.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//年终慰问金
                count = 1;
            }
            i++;
        }
        PageEntity page = new PageEntity();
        page.setList(map);
        page.setFromtable("uf_sydwltxrygzjtbtj");

        return JSON.toJSONString(page);
    }




}
